package com.cs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cs.domain.SysUser;
import com.cs.domain.SysUserRole;
import com.cs.mapper.SysUserMapper;
import com.cs.mapper.SysUserRoleMapper;
import com.cs.service.SysUserRoleService;
import com.cs.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 分页查询后台管理员
     *
     * @param page
     * @param sysUser
     * @return
     */
    @Override
    public Page<SysUser> findSysUserPage(Page<SysUser> page, SysUser sysUser) {
        //排序
        page.addOrder(OrderItem.desc("create_time"));
        Page<SysUser> sysUserPage = sysUserMapper.selectPage(page, new LambdaQueryWrapper<SysUser>().like(StringUtils.hasText(sysUser.getUsername()), SysUser::getUsername, sysUser.getUsername()));
        return sysUserPage;
    }

    /**
     * 添加管理员
     *
     * @param sysUser
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addSysUser(SysUser sysUser) {
        log.info("新增管理员，操作人id：{}",sysUser.getCreateUserId());
        String password = sysUser.getPassword();
        //给密码加密
        String encode = new BCryptPasswordEncoder().encode(password);
        sysUser.setPassword(encode);
        sysUser.setCreateTime(new Date());
        sysUser.setShopId(1L);
        int insert = sysUserMapper.insert(sysUser);
        //添加成功后，添加用户角色关系
        if(insert>0){
            Long userId = sysUser.getUserId();
            //新增的用户角色id
            List<Long> roleIdList = sysUser.getRoleIdList();

            if(!CollectionUtils.isEmpty(roleIdList)){
                List<SysUserRole> sysUserRoles = new ArrayList<>(roleIdList.size()*2);
                //循环用户的角色id 在循环中不能操作添加数据库
                roleIdList.forEach(roleId->{
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(userId);
                    sysUserRole.setRoleId(roleId);
                    sysUserRoles.add(sysUserRole);
                });
                //批量插入用户角色关系
               sysUserRoleService.saveBatch(sysUserRoles);
            }
        }
    }
}
